<?php
/*! \brief
 * This is WFWidgetsNewInfoXmlManager class
 * This is the Class that manipulate file /path/to/repository/widgets_new_info.xml
 * \author William Su
 */
class WFWidgetsNewInfoXmlManager
{
    /**
     * @var WF
     */
    protected $objFramework;
    protected $strPathWidgetsNewInfoXml;
    /**
     * @var WFDOMDocument
     */
    protected $objDOMDocument;
    public function __construct($objFramework){
        $this->objFramework = $objFramework;
        $this->strPathWidgetsNewInfoXml = $this->objFramework->getDocumentRoot() . '/repository/widgets_new_info.xml';
        $this->objDOMDocument = $this->objFramework->getDomFactory()->getDOMDocument();
    }
    /*protected function getPathWidgetsNewInfoXml(){
        $this->strPathWidgetsNewInfoXml = $this->objFramework->
    }*/
    /**
     *\brief Description
     * Create /path/to/repository/widgets_new_info.xml by below parameters
     * @param integer $intWidgetUpdateStatus
     * @param integer $intConfigureWidgetUpdateStatus
     * @param array $arrsWidgetComponentUpdateStatus array (
          0 => 
          array (
            'id' => '15.2',
            'name' => 'SLF_PRODUCT_OFFICESCAN_CE',
            'update_status' => '1',
          ),
          1 => 
          array (
            'id' => '15.1',
            'name' => 'SLF_PRODUCT_OFFICESCAN_CE',
            'update_status' => '1',
          ),
        )
     */
    public function createByCondition($intWidgetUpdateStatus, $intConfigureWidgetUpdateStatus, $arrsWidgetComponentUpdateStatus){
        // we want a nice output
        $this->objDOMDocument->formatOutput = true;
        $elWidgetsNew = $this->objDOMDocument->createElement('widgets_new');
        $this->objDOMDocument->appendChild($elWidgetsNew);
        
        $elWidgetsUpdate = $this->objDOMDocument->createElement('widgets_update');
        $elWidgetsNew->appendChild($elWidgetsUpdate);
        
        $elWidgetUpdateStatusForWidget = $this->objDOMDocument->createElement('widget_update_status', $intWidgetUpdateStatus);
        $elWidgetsUpdate->appendChild($elWidgetUpdateStatusForWidget);
        $strComment = 'widget_type=0,1,2; 0=no need update,1=need enable,2=need update';
        $elWidgetUpdateStatusForWidgetComment = $this->objDOMDocument->createComment($strComment);
        $elWidgetsUpdate->appendChild($elWidgetUpdateStatusForWidgetComment);
        
        $elWidgetUpdateResultForWidget = $this->objDOMDocument->createElement('widget_update_result', '0');
        $elWidgetsUpdate->appendChild($elWidgetUpdateResultForWidget);
        $strComment = 'widget_type=0,1,2; 0=no yet update,1=update success,2=update failed';
        $elWidgetUpdateResultForWidgetComment = $this->objDOMDocument->createComment($strComment);
        $elWidgetsUpdate->appendChild($elWidgetUpdateResultForWidgetComment);
        
        $elWidgetUpdateStatusForConfigureWidget = $this->objDOMDocument->createElement('configure_widget_update_status', $intConfigureWidgetUpdateStatus);
        $elWidgetsUpdate->appendChild($elWidgetUpdateStatusForConfigureWidget);
        $strComment = 'widget_type=3; 0=no need update,1=need enable,2=need update';
        $elWidgetUpdateStatusForConfigureWidgetComment = $this->objDOMDocument->createComment($strComment);
        $elWidgetsUpdate->appendChild($elWidgetUpdateStatusForConfigureWidgetComment);
        
        $elWidgetUpdateResultForConfigureWidget = $this->objDOMDocument->createElement('configure_widget_update_result', '0');
        $elWidgetsUpdate->appendChild($elWidgetUpdateResultForConfigureWidget);
        $strComment = 'widget_type=3; 0=no yet update,1=update success,2=update failed';
        $elWidgetUpdateResultForWidgetComment = $this->objDOMDocument->createComment($strComment);
        $elWidgetsUpdate->appendChild($elWidgetUpdateResultForWidgetComment);
        
        $elWidgetsComponentsUpdate = $this->objDOMDocument->createElement('widget_components_update');
        //, $intConfigureWidgetUpdateStatus
        $elWidgetsNew->appendChild($elWidgetsComponentsUpdate);
        
        $elProductTypes = $this->objDOMDocument->createElement('product_types');
        $elWidgetsComponentsUpdate->appendChild($elProductTypes);
        if(is_array($arrsWidgetComponentUpdateStatus) && $arrsWidgetComponentUpdateStatus){
            foreach($arrsWidgetComponentUpdateStatus as $assocWidgetComponentUpdateStatus){
                $elProductType = $this->objDOMDocument->createElement('product_type');
                $elProductType->setAttribute('id', $assocWidgetComponentUpdateStatus['id']);
                $elProductType->setAttribute('name', $assocWidgetComponentUpdateStatus['name']);
                $elProductTypes->appendChild($elProductType);
                
                $elUpdateStatus = $this->objDOMDocument->createElement('update_status', $assocWidgetComponentUpdateStatus['update_status']);
                $elProductType->appendChild($elUpdateStatus);
                $strComment = '0=no need update,1=need enable,2=need update';
                $elUpdateStatusComment = $this->objDOMDocument->createComment($strComment);
                $elProductType->appendChild($elUpdateStatusComment);
                
                $elUpdateResult = $this->objDOMDocument->createElement('update_result', '0');
                $elProductType->appendChild($elUpdateResult);
                $strComment = '0=no yet update,1=update success,2=update failed';
                $elUpdateResultComment = $this->objDOMDocument->createComment($strComment);
                $elProductType->appendChild($elUpdateResultComment);
            }
        }
        //var_dump($this->objDOMDocument->saveXML());exit();
        $strContentXml = $this->objDOMDocument->saveXML();
        $this->objFramework->getIOFactory()->getFile()->writeDataSafe($this->strPathWidgetsNewInfoXml, 'w+', $strContentXml);
        /*$booResult = $this->objDOMDocument->save($this->strPathWidgetsNewInfoXml);
        if(false === $booResult){
            $message = __CLASS__.'::'.__FUNCTION__.'()=>Occur error when DOMDocument save to file:['.$this->strPathWidgetsNewInfoXml.']';
        }*/
    }
    /**
     *\brief Description
     * - This method will update widgets update_result value in /path/to/repository/widgets_new_info.xml
     * - If saveXML error will throw an Exception.
     */
    public function updateWidgetUpdateResultToWidgetsNewInfoXml($intUpdateResult){
        if(false === $this->isXmlFileExists()){
            return;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $elWidgetsNew[0]->widgets_update->widget_update_result = $intUpdateResult;
        $strContentXml = $elWidgetsNew->asXML();
        $this->objFramework->getIOFactory()->getFile()->writeDataSafe($this->strPathWidgetsNewInfoXml, 'w+', $strContentXml);
        /*$booResult = $elWidgetsNew->saveXML($this->strPathWidgetsNewInfoXml);
        if(false === $booResult){
            $message = __CLASS__.'::'.__FUNCTION__.'()=>saveXML Error! File:'.$this->strPathWidgetsNewInfoXml;
            throw new WFWidgetPoolWidgetPoolManagerException($message);
        }*/
    }
    /**
     *\brief Description
     * - This method will update widgets_new->widgets_update->configure_widget_update_result value in /path/to/repository/widgets_new_info.xml
     * - If saveXML error will throw an Exception.
     */
    public function updateConfigureWidgetUpdateResultToWidgetsNewInfoXml($intUpdateResult){
        if(false === $this->isXmlFileExists()){
            return;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $elWidgetsNew[0]->widgets_update->configure_widget_update_result = $intUpdateResult;
        $strContentXml = $elWidgetsNew->asXML();
        $this->objFramework->getIOFactory()->getFile()->writeDataSafe($this->strPathWidgetsNewInfoXml, 'w+', $strContentXml);
    }
    /**
     *\brief Description
     * - This method will update widgets_new->widget_components_update->product_types->product_type[@id=$mixCategoryProductId]->update_result value in /path/to/repository/widgets_new_info.xml
     * - If saveXML error will throw an Exception.
     */
    public function updateWidgetComponentUpdateResultToWidgetsNewInfoXml($intUpdateResult, $mixCategoryProductId){
        if(false === $this->isXmlFileExists()){
            return;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $elProductType = $elWidgetsNew->xpath('/widgets_new/widget_components_update/product_types/product_type[@id="' . $mixCategoryProductId . '"]');
        $elProductType[0]->update_result = $intUpdateResult;
        $strContentXml = $elWidgetsNew->asXML();
        $this->objFramework->getIOFactory()->getFile()->writeDataSafe($this->strPathWidgetsNewInfoXml, 'w+', $strContentXml);
    }
    /**
     *\brief Description
     * - This method will get widgets_new->widgets_update->widget_update_status value from /path/to/repository/widgets_new_info.xml
     * - If saveXML error will throw an Exception.
     */
    public function getWidgetUpdateStatusFromWidgetsNewInfoXml(){
        $ret = '';
        if(false === $this->isXmlFileExists()){
            return $ret;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $ret = $elWidgetsNew[0]->widgets_update->widget_update_status;
        $ret = (string) $ret;
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get widgets widgets_new->widgets_update->widget_update_result value from /path/to/repository/widgets_new_info.xml
     */
    public function getWidgetUpdateResultFromWidgetsNewInfoXml(){
        $ret = '';
        if(false === $this->isXmlFileExists()){
            return $ret;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $ret = $elWidgetsNew[0]->widgets_update->widget_update_result;
        $ret = (string) $ret;
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get widgets_new->widgets_update->configure_widget_update_status value from /path/to/repository/widgets_new_info.xml
     */
    public function getConfigureWidgetUpdateStatusFromWidgetsNewInfoXml(){
        $ret = '';
        if(false === $this->isXmlFileExists()){
            return $ret;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $ret = $elWidgetsNew[0]->widgets_update->configure_widget_update_status;
        $ret = (string) $ret;
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get widgets_new->widgets_update->configure_widget_update_result value from /path/to/repository/widgets_new_info.xml
     */
    public function getConfigureWidgetUpdateResultFromWidgetsNewInfoXml(){
        $ret = '';
        if(false === $this->isXmlFileExists()){
            return $ret;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $ret = $elWidgetsNew[0]->widgets_update->configure_widget_update_result;
        $ret = (string) $ret;
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get widgets_new->widget_components_update->product_types->product_type[@id=$mixCategoryProductId]->update_status value from /path/to/repository/widgets_new_info.xml
     * - Reason use getWidgetComponentUpdateStatusFromWidgetsNewInfoXml() instead of getArrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml() is make sure interface is consistency with getWidgetUpdateStatusFromWidgetsNewInfoXml() and getConfigureWidgetUpdateStatusFromWidgetsNewInfoXml
     * \par Variable
     * - $ret = array (
                  0 => 
                  array (
                    '@attributes' => 
                    array (
                      'id' => '15.2',
                      'name' => 'OfficeScan Server',
                    ),
                    'update_status' => NULL,
                    'comment' => NULL,
                  ),
                  1 => 
                  array (
                    '@attributes' => 
                    array (
                      'id' => '15.4',
                      'name' => 'OfficeScan Client',
                    ),
                    'update_status' => '1',
                    'comment' => NULL,
                  ),
                )
     * @return array arrs
     */
    public function getWidgetComponentUpdateStatusFromWidgetsNewInfoXml(){
        $ret = $this->getArrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml();
        return $ret;
    }
    /**
     * Alias of getWidgetComponentUpdateStatusFromWidgetsNewInfoXml
     */
    public function getArrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml(){
        $ret = array();
        if(false === $this->isXmlFileExists()){
            return $ret;
        }
        $elWidgetsNew = new SimpleXMLElement($this->strPathWidgetsNewInfoXml, null, true);
        $elArrsProductType = $elWidgetsNew->xpath('/widgets_new/widget_components_update/product_types/product_type');
        $ret = $this->objFramework->getTypeFactory()->getObject()->convertObject2Array($elArrsProductType);
        return $ret;
    }
    /**
     *\brief Description 
     * - Check is /path/to/repository/widgets_new_info.xml exists or not, if exists return true else return false
     */
    protected function isXmlFileExists(){
        $isFileExists = $this->objFramework->getIOFactory()->getFile()->isFileExists($this->strPathWidgetsNewInfoXml);
        return $isFileExists;
    }
}